package chenji.gunapi.gun;

import java.util.LinkedHashSet;

import org.bukkit.entity.Player;

import chenji.gunapi.pluginmessage.PluginMessageUntl;
import chenji.gunapi.pluginmessage.packages.OutChengeSightsPackage;

public class Sight implements Cloneable {
	private int index = 0;
	private boolean close;
	private SingleSight nowSingleSight;
	private LinkedHashSet<SingleSight> miao = new LinkedHashSet<>();
	private Player p;
	private boolean isOpening;

	private Sight(LinkedHashSet<SingleSight> miao) {
		this.miao = miao;
	}

	public void addSingleSight(SingleSight s) {
		this.miao.add(s);
	}

	public void chengeSight() {
		index++;
		if (this.index > this.miao.size()) {
			this.close = true;
		} else {
			nowSingleSight = this.getByIndex(index);
		}
		this.update();
	}

	public void update() {
		if (this.close) {
			this.sendClosePackage();
			return;
		}
		this.nowSingleSight.open(p);
		this.isOpening = true;

	}

	private void sendClosePackage() {
		OutChengeSightsPackage pack = new OutChengeSightsPackage();
		PluginMessageUntl.getPluginMessageUntl().getPackageManager().sendPackage(p, pack);
		this.isOpening = false;
	}

	private SingleSight getByIndex(int index) {
		int now = 0;
		for (SingleSight sa : this.miao) {
			now++;
			if (now == index) {
				return sa;
			}

		}
		return null;
	}

	public void closeSight() {
		this.sendClosePackage();
	}

	private Sight setPlayer(Player p) {
		this.p = p;
		return this;
	}

	public Sight clone(Player p) {
		return new Sight(this.miao).setPlayer(p);

	}

	public boolean isOpneing() {
		return this.isOpening;
	}

	public void open() {
		this.nowSingleSight.open(this.p);

	}
}

class SingleSight {
	private float view;
	private String address;

	public SingleSight(float view, String address) {
		this.view = view;
		this.address = address;
	}

	public void open(Player p) {
		OutChengeSightsPackage pack = new OutChengeSightsPackage(view, address, false);
		PluginMessageUntl.getPluginMessageUntl().getPackageManager().sendPackage(p, pack);
	}

}